package com.k4rthik.clipall.network;

import com.k4rthik.clipall.commons.Logger;
import com.k4rthik.clipall.network.profile.ProfileManager;

import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

/**
 * Created by IntelliJ IDEA.
 * User: k4rthik
 * Date: 3/24/12
 * Time: 1:36 PM
 */

public class Connections extends Thread
{
    static Map<UUID, Socket> clientConnections = new HashMap<UUID, Socket>();
    static Map<UUID, Socket> serverConnections = new HashMap<UUID, Socket>();

    public static void addClientConnection(Socket newConnection)
    {
        //Generate new UUID for connection
        UUID newUUID = UUID.randomUUID();
        while(clientConnections.containsKey(newUUID) || serverConnections.containsKey(newUUID)) {
            newUUID = UUID.randomUUID();
        }

        //Add connection to list and active profile
        clientConnections.put(newUUID, newConnection);
        ProfileManager.getInstance().addConnectionToActiveProfile(newUUID);
    }

    public static void addServerConnection(Socket newConnection)
    {
        //Generate new UUID for connection
        UUID newUUID = UUID.randomUUID();
        while(clientConnections.containsKey(newUUID) || serverConnections.containsKey(newUUID)) {
            newUUID = UUID.randomUUID();
        }

        //Add connection to list
        serverConnections.put(newUUID, newConnection);
        ProfileManager.getInstance().addConnectionToActiveProfile(newUUID);
    }

    public static Map<UUID, Socket> getClientConnections()
    {
        return clientConnections;
    }

    public static Map<UUID, Socket> getServerConnections()
    {
        return serverConnections;
    }

    @Override
    public void run()
    {
        while(true) {

            //Test status of client connections.
            if(clientConnections != null) {
                Iterator<UUID> clientIt = clientConnections.keySet().iterator();

                while(clientIt.hasNext()) {
                    UUID clientUUID = clientIt.next();
                    Socket clientSocket = clientConnections.get(clientUUID);
                    try
                    {
                        clientSocket.getInputStream().available();
                    } catch (Exception e) {
                        Logger.log("Client "+clientUUID.toString()+" closed connection.");
                        clientConnections.remove(clientUUID);
                    }
                }
            }

            //Test status of server connections.
            if(serverConnections != null) {
                Iterator<UUID> serverIt = serverConnections.keySet().iterator();

                while(serverIt.hasNext()) {
                    UUID serverUUID = serverIt.next();
                    Socket serverSocket = serverConnections.get(serverUUID);
                    try
                    {
                        serverSocket.getInputStream().available();
                    } catch (Exception e) {
                        Logger.log("Server "+serverUUID.toString()+" closed connection.");
                        serverConnections.remove(serverUUID);
                    }
                }
            }
        }
    }
}
